//
//  TVCommodityPurchaseCell.swift
//  StarTimesON
//
//  Created by Toj on 12/1/22.
//  Copyright © 2022 StarTimes. All rights reserved.
//

import UIKit

protocol TVCommodityPurchaseCellDelegate: NSObjectProtocol {
    func commodityPurchaseCell(_ view: TVCommodityPurchaseCell, didSelected commodity: TVAdditionalCommodityCellModel)
}

class TVCommodityPurchaseCell: UICollectionViewCell {
    
    weak var delegate: TVCommodityPurchaseCellDelegate?
    
    var cellModel: TVPurchaseCellModel? { willSet {
        guard cellModel != newValue else { return }
        cellModel?.isLoaded = false
        
    } didSet {
        guard let cellModel = cellModel else  { return }
        
        titleLable.text = cellModel.title
        tipsLabel.text = cellModel.brief
        collectionView.reloadData()
         
        if cellModel.isLoaded { return }
        cellModel.isLoaded = true
        
        guard cellModel.cellModels.count > 0 else { return }
        collectionView.layoutIfNeeded()

        let indexPath = IndexPath(row: 0, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var gaColors: [CGColor] = {
        let leftColor = "#FF7200".to_color.cgColor
        let rightColor = "#FF322C".to_color.cgColor
        let cls = [leftColor, rightColor]
        return cls
    }()
    
    private var isTitleViewAddGA = false
    override func layoutSubviews() {
        super.layoutSubviews()
        
        setupMakeLayoutSubviews()
        
        guard !isTitleViewAddGA else { return }
        layoutIfNeeded()
        titleView.gd_add(colors: gaColors)
        isTitleViewAddGA = true
    }
    
    private lazy var purchaseView: UIView = {
        let view = UIView()
        view.st_cornerRadius = 8
        view.backgroundColor = "#FF322C".to_color
        return view
    }()
    
    private lazy var titleView = UIView()
    
    private lazy var iconImageView: UIImageView = {
        let view = UIImageView()
        view.image = "commodity_tv_purchase_icon".to_image
        return view
    }()
    
    private lazy var titleLable: UILabel = {
        let view = UILabel()
        view.textColor = .white
        view.font = 16.to_systemFont
        return view
    }()
    
    private lazy var commodityView: UIView = {
        let view = UIView()
        view.st_cornerRadius = 6
        view.backgroundColor = .white
        return view
    }()
    
    private lazy var tipsLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.font = 10.ui_systemFont
        view.textColor = "#999999".to_color
        return view
    }()
    
    private lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize(width: 88, height: 112)
        
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.scrollsToTop = true
        view.showsVerticalScrollIndicator = false
        view.showsHorizontalScrollIndicator = false
        view.backgroundColor = .white
        view.contentInset = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
        view.register(TVAdditionalCellModel.cell_class, forCellWithReuseIdentifier: TVAdditionalCellModel.cell_identifier)
        view.dataSource = self
        view.delegate = self
        return view
    }()
}

// MARK: - 代理事件 UICollectionViewDataSource
extension TVCommodityPurchaseCell: UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cellModel?.cellModels.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TVAdditionalCellModel.cell_identifier, for: indexPath, TVAdditionalCellModel.self)
        cell.cellModel = cellModel?.cellModels[indexPath.row]
        return cell
    }
}

// MARK: - 代理事件 UICollectionViewDataSource
extension TVCommodityPurchaseCell: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        guard let cell = collectionView.cellForItem(at: indexPath) as? TVAdditionalCellModel, let commodity = cell.cellModel else { return }
        delegate?.commodityPurchaseCell(self, didSelected: commodity)
        
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
    }
}

extension TVCommodityPurchaseCell {
    
    private func setupMakeAddSubviews() {
        
        contentView.addSubview(purchaseView)
        purchaseView.addSubview(commodityView)
        commodityView.addSubview(tipsLabel)
        commodityView.addSubview(collectionView)
        
        purchaseView.addSubview(titleView)
        titleView.addSubview(iconImageView)
        titleView.addSubview(titleLable)
    }
    
    private func setupMakeLayoutSubviews() {
        purchaseView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        titleView.snp.remakeConstraints { make in
            make.height.equalTo(44)
            make.width.left.top.equalTo(purchaseView)
        }
        
        commodityView.snp.remakeConstraints { make in
            make.left.right.bottom.equalTo(purchaseView).inset(2)
            make.top.equalTo(titleView.snp.bottom).offset(-4)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalTo(titleView)
            make.width.height.equalTo(24)
        }
        
        titleLable.snp.makeConstraints { make in
            make.left.equalTo(iconImageView.snp.right).offset(5)
            make.centerY.equalTo(iconImageView)
            make.right.equalTo(titleView.snp.right)
        }
        
        collectionView.snp.makeConstraints { make in
            make.left.right.equalTo(commodityView)
            make.bottom.equalTo(commodityView.snp.bottom).offset(-20)
            make.height.equalTo(112)
        }
        
        tipsLabel.snp.makeConstraints { make in
            make.top.equalTo(titleView.snp.bottom)
            make.bottom.equalTo(collectionView.snp.top)
            make.left.right.equalTo(collectionView).inset(12)
        }
    }
}
